package com.hnkywl.sts.service.organ;

import com.google.gson.Gson;
import com.hnkywl.sts.dao.organ.CkDao;
import com.hnkywl.sts.dao.organ.NbgsDao;
import com.hnkywl.sts.entity.organ.Ck;
import com.hnkywl.sts.service.base.BmService;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-3-28
 * Time: 下午3:31
 * To change this template use File | Settings | File Templates.
 */
@Service
public class CkService {
    private BmService bmService;
    private CkDao ckDao;
    private NbgsDao nbgsDao;
    private NbgsService nbgsService;

    /**
     * 查询仓库列表
     *
     * @param ckmc     仓库名称
     * @param ssgsjg   所属公司机构
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Transactional
    public String findAllCk(String ckmc, String ssgsjg, Long ssgsjgId, int pageNo, int pageSize) {
        Page<Ck> ckPage;
        String ids = "";
        List<Long> nbgsIds = new ArrayList<Long>();
        if (ssgsjgId != null && ssgsjg.equals(nbgsDao.get(ssgsjgId).getJc())) {
            nbgsIds = nbgsService.findAllNbgsById(ssgsjgId, nbgsIds);
            for (Long nbgsid : nbgsIds) {
                ids += nbgsid + ",";
            }
            ids = ids.substring(0, ids.length() - 1);
            ckPage = this.ckDao.findCkList(ckmc, ssgsjg, ids, pageNo, pageSize);
        } else {
            ckPage = this.ckDao.findCkList(ckmc, ssgsjg, ids, pageNo, pageSize);
        }
        List<Ck> ckList = ckPage.getResult() == null ? new ArrayList<Ck>() : ckPage.getResult();
        String ckJson = BaseUtils.fetchCollectionToJson(ckList, "id", "bh", "mc", "lxr", "ckjb", "ssgsjg.id", "ssgsjg.jc", "dz");
        return "{\"total\":" + ckPage.getTotalCount() + ",\"rows\":[" + ckJson + "]}";
    }

    @Transactional
    public String findAllCkmc(Long ssgsjgId) {
        Gson gson = new Gson();
        List list = this.ckDao.findCkList(ssgsjgId);
        String json = gson.toJson(list);
        return json;
    }

    @Transactional
    public String findAllBgCkmc() {
        Gson gson = new Gson();
        List list = this.ckDao.findBgCkList();
        String json = gson.toJson(list);
        return json;
    }

    //findYwsfcCkList
    @Transactional
    public String findYwsfcCkList() {
        Gson gson = new Gson();
        List list = this.ckDao.findYwsfcCkList();
        String json = gson.toJson(list);
        return json;
    }

    @Transactional
    public String findSerchCkmc(Long ssgsjgId) {
        Gson gson = new Gson();
        List list = this.ckDao.findCkSearch(ssgsjgId);
        String json = gson.toJson(list);
        return json;
    }

    @Transactional
    public String sskcCkmcForSskc(Long ssgsjgId) {
        Gson gson = new Gson();
        List list = this.ckDao.sskcCkmcForSskc(ssgsjgId);
        String json = gson.toJson(list);
        return json;
    }

    @Transactional
    public String findSerchCkmcForGsjg(Long ssgsjgId) {
        Gson gson = new Gson();
        List list = this.ckDao.findCkSearchForSearch(ssgsjgId);
        String json = gson.toJson(list);
        return json;
    }

    //获取仓库编码
    public String getCkbm() {
        return bmService.getCkbm("CK");
    }

    /**
     * 查询仓库明细
     *
     * @param id 仓库ID
     * @return
     */
    @Transactional
    public Ck findCkDetail(Long id) {
        return this.ckDao.get(id);
    }

    /**
     * 根据仓库编码查询仓库
     *
     * @param bh
     * @return
     */
    @Transactional
    public Ck findCkByBh(String bh) {
        return this.ckDao.findByBh(bh);
    }

    /**
     * 保存仓库信息
     *
     * @param ck 仓库实体
     * @return
     */
    @Transactional
    public void saveCk(Ck ck) {
        if (StringUtils.isEmpty(ck.getBh())) {
            String bm = this.getCkbm();
            ck.setBh(bm);
        }
        nullId(ck);
        ck.setDateCreated(new Date());
        ck.setLastUpdated(ck.getDateCreated());
        this.ckDao.save(ck);
    }

    /**
     * 更新仓库信息
     *
     * @param ck 仓库实体
     * @return
     */
    @Transactional
    public void updateCk(Ck ck) {
        Ck clone = this.findCkDetail(ck.getId());
        clone = (Ck) ReflectionUtils.getInstance(clone, ck);
        clone.setLastUpdated(new Date());
        this.ckDao.save(clone);
    }

    /**
     * 删除仓库信息
     *
     * @param id 仓库id
     * @return
     */
    @Transactional
    public void deleteCk(Long id) {
        Ck ck = ckDao.get(id);
        if (!ck.getCgdList().isEmpty()) {
            throw new RuntimeException("此仓库已被使用，不能删除！");
        }
        if (!ck.getXsdList().isEmpty()) {
            throw new RuntimeException("此仓库已被使用，不能删除！");
        }
        ck.setLastUpdated(new Date());
        ck.setDeleted(1);
        this.ckDao.save(ck);
    }

    /**
     * 关联判断
     *
     * @param ck
     */
    private void nullId(Ck ck) {
        if (ck != null && ck.getSsgsjg() != null && ck.getSsgsjg().getId() == null)
            ck.setSsgsjg(null);

    }

    //查找下一个排序号
    @Transactional
    public Integer findNextPxh() {
        return this.ckDao.findMaxPxh() == null ? 1 : this.ckDao.findMaxPxh() + 1;
    }

    /**
     * ajax远程验证
     *
     * @param value 验证值
     * @param field 验证字段
     * @return
     */
    public boolean ajaxValidate(Long id, String value, Object field) {
        return ckDao.checkUnique(id, value, field);
    }

    @Autowired
    public void setBmService(BmService bmService) {
        this.bmService = bmService;
    }

    @Autowired
    public void setCkDao(CkDao ckDao) {
        this.ckDao = ckDao;
    }

    @Autowired
    public void setNbgsDao(NbgsDao nbgsDao) {
        this.nbgsDao = nbgsDao;
    }

    @Autowired
    public void setNbgsService(NbgsService nbgsService) {
        this.nbgsService = nbgsService;
    }
}
